package com.demo.user.handler;

import com.demo.user.exception.UserException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义异常返回信息
 *
 * @author YT
 * @date 2019-05-23
 */

/**
 * @ControllerAdvice 作为处理 controller 产生异常后执行的处理类，不作为服务访问
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    /**
     * @return
     * @ExceptionHandler 处理当 controller 产生 RuntimeException 时执行的方法
     */
    @ExceptionHandler(UserException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> handlerUserException(UserException ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("id", ex.getExceptionToDo());
        result.put("message", ex.getMessage());
        return result;
    }
}
